{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "866780b4-2d16-401f-bd1e-9fdb26cc5fb1",
   "metadata": {},
   "source": [
    "# Stacked Bar Chart\n",
    "\n",
    "A stacked bar chart showing the total count of penguins per species and how they break down by sex. This example uses the `penguins` dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33e5a2f2-a582-44cb-a957-16b0a1a21b0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.pandas  # noqa\n",
    "\n",
    "df = hvplot.sampledata.penguins('pandas')\n",
    "grouped = df.groupby(['species', 'sex'], observed=True).size().reset_index(name='count')\n",
    "\n",
    "grouped.hvplot.bar(\n",
    "    x='species',\n",
    "    y='count',\n",
    "    by='sex',\n",
    "    stacked=True,\n",
    "    title='Stacked Bar Chart (Bokeh)',\n",
    "    xlabel='Species',\n",
    "    ylabel='Count',\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3eb0aba8-6c8a-4f28-b0ef-148254c838b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.pandas  # noqa\n",
    "hvplot.extension('matplotlib')\n",
    "\n",
    "df = hvplot.sampledata.penguins('pandas')\n",
    "grouped = df.groupby(['species', 'sex'], observed=True).size().reset_index(name='count')\n",
    "\n",
    "grouped.hvplot.bar(\n",
    "    x='species',\n",
    "    y='count',\n",
    "    by='sex',\n",
    "    stacked=True,\n",
    "    title='Stacked Bar Chart (Matplotlib)',\n",
    "    xlabel='Species',\n",
    "    ylabel='Count',\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c76e94a1-abba-4520-8e3b-f949021fb06b",
   "metadata": {},
   "outputs": [],
   "source": [
    "grouped"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7490270-b215-4149-b2e8-9b416ec046fe",
   "metadata": {},
   "source": [
    ":::{seealso}\n",
    "- [Bar Plots reference documentation](../../ref/api/manual/hvplot.hvPlot.bar.ipynb).\n",
    ":::"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
